package si.cir.util;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.TreeMap;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import si.cir.entities.Forum;
import si.cir.entities.IzpitniRok;
import si.cir.entities.Napoved;
import si.cir.entities.Novica;
import si.cir.entities.Stuk;
import si.cir.entities.UmNovica;
import si.cir.entities.Vreme;
import si.cir.entities.Zagovor;

public class WSclient {

	private static SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();

	public static List<Novica> getNovice() {
		try{
			URL url = new URL("http://164.8.251.99:9793/services/cir2012/SiNf_FeriNews/feriNovice");
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		
			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				NoviceHandler handler = new NoviceHandler();
				SAXParser parser = saxParserFactory.newSAXParser();
				parser.parse(connection.getInputStream(), handler);
				return handler.getNovice();
			}
		}catch (Exception e) {
		}
		return null;
	}
	
	public static List<Stuk> getProgram() {
		try{
			URL url = new URL("http://164.8.251.99:9793/services/cir2012/SiNf_StukProg/vrniProgramDogajanja");
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		
			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				StukHandler handler = new StukHandler();
				SAXParser parser = saxParserFactory.newSAXParser();
				parser.parse(connection.getInputStream(), handler);
				return handler.getNovice();
			}
		}catch (Exception e) {
		}
		return null;
	}
	
	public static List<Forum> getForum() {
		try{
			URL url = new URL("http://164.8.251.99:9793/services/cir2012/SiNf_FeriForum/vrniZadnjeForum");
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		
			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				ForumHandler handler = new ForumHandler();
				SAXParser parser = saxParserFactory.newSAXParser();
				parser.parse(connection.getInputStream(), handler);
				return handler.getObjave();
			}
		}catch (Exception e) {
		}
		return null;
	}
	
	
	public static List<Zagovor> getZagovor() {
		try{
			URL url = new URL("http://164.8.251.99:9793/services/cir2012/FeriZagovori/FeriZagovori");
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		
			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				ZagovorHandler handler = new ZagovorHandler();
				SAXParser parser = saxParserFactory.newSAXParser();
				parser.parse(connection.getInputStream(), handler);
				return handler.getZagovori();
			}
		}catch (Exception e) {
		}
		return null;
	}
	
	public static List<UmNovica> getUmNovice() {
		try{
			URL url = new URL("http://164.8.251.99:9793/services/cir2012/SiNf_UMNews/SiNf_UMNews");
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		
			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				UmNoviceHandler handler = new UmNoviceHandler();
				SAXParser parser = saxParserFactory.newSAXParser();
				parser.parse(connection.getInputStream(), handler);
				return handler.getUmNovice();
			}
		}catch (Exception e) {
		}
		return null;
	}
	
	public static List<Novica> getSoumNovice() {
		try{
			URL url = new URL("http://164.8.251.99:9793/services/cir2012/SiNf_SoumNews/vrniSoumNovice");
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		
			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				SoumNoviceHandler handler = new SoumNoviceHandler();
				SAXParser parser = saxParserFactory.newSAXParser();
				parser.parse(connection.getInputStream(), handler);
				return handler.getNovice();
			}
		}catch (Exception e) {
		}
		return null;
	}
	
	public static List<Vreme> getVreme(String mesto) {
		try{
			URL url = new URL("http://164.8.251.99:9793/services/cir2012/SiNf_Vreme/vrniVremeZaMesto?mesto="+mesto);
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		
			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				VremeHandler handler = new VremeHandler();
				SAXParser parser = saxParserFactory.newSAXParser();
				parser.parse(connection.getInputStream(), handler);
				return handler.getVreme();
			}
		}catch (Exception e) {
		}
		return null;
	}
	public static List<Napoved> getNapovedi(String mesto) {
		try{
			URL url = new URL("http://164.8.251.99:9793/services/cir2012/SiNf_Vreme/vrniVremeZaMesto?mesto="+mesto);
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		
			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				VremeHandler handler = new VremeHandler();
				SAXParser parser = saxParserFactory.newSAXParser();
				parser.parse(connection.getInputStream(), handler);
				return handler.getNapoved();
			}
		}catch (Exception e) {
		}
		return null;
	}
	
	public static TreeMap<String, String> getPredmeti(String name){
		try{
			URL url = new URL("http://164.8.251.99:9793/services/cir2012/SiNf_aips/getPredmetID?imeAliIdPredmeta=" + name.replace(" ", "%20"));
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		
			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				PredmetiHandler handler = new PredmetiHandler();
				SAXParser parser = saxParserFactory.newSAXParser();
				parser.parse(connection.getInputStream(), handler);
				return handler.getPredmeti();
			}
		}catch (Exception e) {
		}
		return null;	
	}
	
	public static List<IzpitniRok> getIzpitniRoki(String predmetId){
		try{
			URL url = new URL("http://164.8.251.99:9793/services/cir2012/SiNf_aips/getIzpitniRokiPoPredmetu?predmetID=" + predmetId);
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		
			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				AipsHandler handler = new AipsHandler();
				SAXParser parser = saxParserFactory.newSAXParser();
				parser.parse(connection.getInputStream(), handler);
				return handler.getRoki();
			}
		}catch (Exception e) {
		}
		return null;
	}
}
